package com.blog.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	
	private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	public static String encrypt(String pwd) {
		byte[] bs = pwd.getBytes();
		String rtn = null;

		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(bs);
			byte tmp[] = md5.digest();

			StringBuffer buf = new StringBuffer();
			for (int i = 0; i < 16; i++) {
				byte b = tmp[i];
				buf.append(HEX_DIGITS[b >>> 4 & 0xf]);
				buf.append(HEX_DIGITS[b & 0xf]);
			}
			rtn = buf.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return rtn;
	}

	public static void main(String[] args) {
		String encrypt = MD5.encrypt("Admin");
		System.out.println(encrypt + "---" + encrypt.length());
		System.out.println("499cf741ecd3aa9a67dab42ae058d7d4".length());
	}
}
